import logging

import pytest

from resource.pom.launcher import Launcher
from utils.manager import Images


class Test:

    @pytest.mark.parametrize('setup_teardown', [None], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1、长按相机应用')
        device.refresh_layout()
        camera = device.get_element_by_text('相机')
        cx, cy = device.center_of_element(camera)
        device.long_click(cx, cy - 50)
        device.refresh_layout()
        logging.info('预期结果1、弹出选项弹框')
        device.assert_text_exist('打开')
        device.assert_text_exist('服务卡片')
        device.assert_text_exist('卡片中心')
        device.assert_text_exist('卸载')

        logging.info('步骤2、点击卡片中心')
        card_center = device.get_element_by_text('卡片中心')
        device.click_element(card_center)
        logging.info('预期结果2、进入卡片中心页面')
        device.wait(2)
        try:
            device.assert_current_window(Launcher.form_service_view)
            device.refresh_layout()
            device.assert_text_exist('服务卡片')
            card = device.save_snapshot_to_local('card.jpeg')
            Images.crop_picture(card)
            similarity = Images.compare_image_similarity(Launcher.service_card, card)
            assert similarity > 0.6, '服务卡片显示异常'
        except:
            device.refresh_layout()

        logging.info('步骤3、点击相机')
        gallery = device.get_element_by_text('相机')
        device.click_element(gallery)
        device.wait(1)
        logging.info('预期结果3、进入相机卡片列表页')
        device.assert_current_window(Launcher.form_manager_view)
        device.refresh_layout()
        device.assert_text_exist('相机')
        device.assert_text_exist('添加到桌面')

        logging.info('步骤4、点击添加到桌面')
        device.click_element(device.get_element_by_text('添加到桌面'))
        logging.info('预期结果4、相机服务卡片成功添加到桌面')
        device.refresh_layout()
        device.assert_type_exist('FormComponent')
